﻿using Common;
using DadayunApi.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace DadayunApi
{
    public partial class ReportApi:Api
    {
        public ReportApi(AppAuthBase authBase, string baseUri) : base(authBase, baseUri)
        {
        }

        /// <summary>
        /// 获取单据模板信息列表
        /// </summary>
        /// <returns></returns>
        public List<ReportTemplateInfo> GetReportTemplateInfoes(out int totalCount,int start = 0, int limit = 0, string sort = "Name", IEnumerable<ReportFilter> filter=null)
        {
            List<ReportTemplateInfo> obj = null;
            var uri = "/v1/report/templates?start="+start+"&limit="+limit+"&count=true&sort="+sort;
            if (filter != null)
            {
                JsonSerializerSettings settings = new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                };
                string sfilter = JsonConvert.SerializeObject(filter, settings);
                uri += "&filter=" + sfilter;
            }
            totalCount= GetTotalCount(uri);
            if (limit == 0) limit = totalCount;
            //var ans = HttpClientHelper.HttpClientGetSync(baseUri, uri, out string json, httpgetTokenHeader);
            httpStatus = HttpGet(uri);            
            if (httpStatus == HttpStatusCode.OK)
            {
                obj = JsonConvert.DeserializeObject<List<ReportTemplateInfo>>(responseJson);
            }
            return obj;
        }

        /// <summary>
        /// 获取报表模板
        /// </summary>
        /// <param name="idOrName"></param>
        /// <returns></returns>
        public ReportTplDetail GetReportTemplateDetails(string idOrName)
        {
            ReportTplDetail obj=null;
            var uri = "v1/report/templates/" + idOrName;
            httpStatus = HttpGet(uri);           
            if (httpStatus == HttpStatusCode.OK)
            {
                obj = JsonConvert.DeserializeObject<ReportTplDetail>(responseJson);
            }
            return obj;
        }


        /// <summary>
        /// 查询报表数据
        /// </summary>
        /// <param name="idOrName">报表模板Id或者报表模板名称</param>
        /// <param name="totalCount">报表总条数</param>
        /// <param name="keyOption">输入的fields或filter的条件已什么为键值</param>
        /// <param name="start">默认0，从0开始读取</param>
        /// <param name="limit">默认0，取出所有的记录</param>
        /// <param name="groupby"></param>
        /// <param name="sort"></param>
        /// <param name="fields">返回字段，多个字段用半角逗号分割</param>
        /// <param name="filter">筛选条件json字符串</param>
        /// <param name="sampleStep">对于地图服务请求的超量数据，按照设定的采样率，每隔sampleStep条采用一条。</param>
        /// <param name="sortByDistanceJson">请求对报表数据按距离排序,limit将设置为3000。</param>
        /// <param name="aggResult">返回聚合结果，报表类型为简易报表或组合报表可用。默认值: false</param>
        /// <returns>以json格式返回查询结果。后续反序列化为报表实例</returns>
        public string  GetReportInJson(string idOrName,out int totalCount, EReportKeyOptions keyOption = EReportKeyOptions.FieldMap, 
            IEnumerable< ReportFilter> filter = null, int start = 0, int limit = 0, string groupby = "", string sort = "",
            string fields = "", int sampleStep = 0, string sortByDistanceJson = "", bool aggResult = false)
        {
            totalCount = 0;
            string uri = "v1/report/templates/" + idOrName + "/search?count=true&aggResult=" + aggResult+ "&start=" + start;
            uri += "&KeyOption=" + (int)keyOption;
            
            if (!string.IsNullOrEmpty(sort)) uri += "&sort=" + sort;
            if (!string.IsNullOrEmpty(groupby)) uri += "&groupby=" + groupby;
            if (!string.IsNullOrEmpty(fields)) uri += "&fields=" + fields;
            
            if (sampleStep!=0) uri += "&sampleStep=" + sampleStep;
            if (!string.IsNullOrEmpty(sortByDistanceJson)) uri += "&sortByDistanceJson=" + sortByDistanceJson;
            
            if (filter != null)
            {
                JsonSerializerSettings settings = new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                };
                string sfilter = JsonConvert.SerializeObject(filter, settings);
                uri += "&filter=" + sfilter;
            }

            #region 设置limit值，如果为默认值0，则设置为最大值
            totalCount = GetTotalCount(uri);
            if(totalCount>0)
            {
                if (limit == 0)
                {
                    limit = totalCount;
                }
                uri += "&limit=" + limit;
                #endregion
                httpStatus = HttpGet(uri);

                if (httpStatus == HttpStatusCode.OK)
                {
                    return responseJson;
                }
            }
            
            return "";
        }
        
    }
    
}
